using System;
using System.Collections;
using System.IO;
using System.Text;

namespace SMS.Gateway.Libs
{
    public class ServiceIndication
    {
        // Fields
        public ServiceIndicationAction Action;
        public const byte ATTRIBUTESTARTTOKEN_action_signal_delete = 9;
        public const byte ATTRIBUTESTARTTOKEN_action_signal_high = 8;
        public const byte ATTRIBUTESTARTTOKEN_action_signal_low = 6;
        public const byte ATTRIBUTESTARTTOKEN_action_signal_medium = 7;
        public const byte ATTRIBUTESTARTTOKEN_action_signal_none = 5;
        public const byte ATTRIBUTESTARTTOKEN_class = 0x12;
        public const byte ATTRIBUTESTARTTOKEN_created = 10;
        public const byte ATTRIBUTESTARTTOKEN_href = 11;
        public const byte ATTRIBUTESTARTTOKEN_href_http = 12;
        public const byte ATTRIBUTESTARTTOKEN_href_http_www = 13;
        public const byte ATTRIBUTESTARTTOKEN_href_https = 14;
        public const byte ATTRIBUTESTARTTOKEN_href_https_www = 15;
        public const byte ATTRIBUTESTARTTOKEN_si_expires = 0x10;
        public const byte ATTRIBUTESTARTTOKEN_si_id = 0x11;
        public const byte ATTRIBUTEVALUETOKEN_com = 0x85;
        public const byte ATTRIBUTEVALUETOKEN_edu = 0x86;
        public const byte ATTRIBUTEVALUETOKEN_net = 0x87;
        public const byte ATTRIBUTEVALUETOKEN_org = 0x88;
        private static Hashtable attributeValueTokens;
        public DateTime CreatedAt;
        public const byte DOCUMENT_DTD_ServiceIndication = 5;
        public DateTime ExpiresAt;
        public string Href;
        private static Hashtable hrefStartTokens = new Hashtable();
        public const byte TAGTOKEN_indication = 6;
        public const byte TAGTOKEN_info = 7;
        public const byte TAGTOKEN_item = 8;
        public const byte TAGTOKEN_si = 5;
        public string Text;

        // Methods
        static ServiceIndication()
        {
            hrefStartTokens.Add("https://www.", (byte)15);
            hrefStartTokens.Add("http://www.", (byte)13);
            hrefStartTokens.Add("https://", (byte)14);
            hrefStartTokens.Add("http://", (byte)12);
            attributeValueTokens = new Hashtable();
            attributeValueTokens.Add(".com/", (byte)0x85);
            attributeValueTokens.Add(".edu/", (byte)0x86);
            attributeValueTokens.Add(".net/", (byte)0x87);
            attributeValueTokens.Add(".org/", (byte)0x88);
        }

        public ServiceIndication(string href, string text, ServiceIndicationAction action)
        {
            this.Href = href;
            this.Text = text;
            this.Action = action;
        }

        public ServiceIndication(string href, string text, DateTime createdAt, DateTime expiresAt) : this(href, text, ServiceIndicationAction.NotSet)
        {
            this.CreatedAt = createdAt;
            this.ExpiresAt = expiresAt;
        }

        public ServiceIndication(string href, string text, DateTime createdAt, DateTime expiresAt, ServiceIndicationAction action) : this(href, text, action)
        {
            this.CreatedAt = createdAt;
            this.ExpiresAt = expiresAt;
        }

        protected byte GetActionToken(ServiceIndicationAction action)
        {
            switch (action)
            {
                case ServiceIndicationAction.signal_low:
                    return 6;

                case ServiceIndicationAction.signal_medium:
                    return 7;

                case ServiceIndicationAction.signal_high:
                    return 8;

                case ServiceIndicationAction.delete:
                    return 9;
            }
            return 5;
        }

        public byte[] GetWBXMLBytes()
        {
            MemoryStream stream = new MemoryStream();
            stream.WriteByte(1);
            stream.WriteByte(5);
            stream.WriteByte(0x6a);
            stream.WriteByte(0);
            stream.WriteByte(WBXML.SetTagTokenIndications(5, false, true));
            stream.WriteByte(WBXML.SetTagTokenIndications(6, true, true));
            int startIndex = 0;
            byte num2 = 11;
            foreach (string str in hrefStartTokens.Keys)
            {
                if (this.Href.StartsWith(str))
                {
                    num2 = (byte)hrefStartTokens[str];
                    startIndex = str.Length;
                    break;
                }
            }
            stream.WriteByte(num2);
            this.WriteInlineString(stream, this.Href.Substring(startIndex));
            if (this.Action != ServiceIndicationAction.NotSet)
            {
                stream.WriteByte(this.GetActionToken(this.Action));
            }
            stream.WriteByte(1);
            this.WriteInlineString(stream, this.Text);
            stream.WriteByte(1);
            stream.WriteByte(1);
            return stream.ToArray();
        }

        protected void WriteDate(MemoryStream stream, DateTime date)
        {
            byte[] buffer = new byte[7];
            buffer[0] = (byte)(date.Year / 100);
            buffer[1] = (byte)(date.Year % 100);
            buffer[2] = (byte)date.Month;
            buffer[3] = (byte)date.Day;
            int count = 4;
            if (date.Hour > 0)
            {
                buffer[4] = (byte)date.Hour;
                count = 5;
            }
            if (date.Minute > 0)
            {
                buffer[5] = (byte)date.Minute;
                count = 6;
            }
            if (date.Second > 0)
            {
                buffer[6] = (byte)date.Second;
                count = 7;
            }
            stream.WriteByte(0xc3);
            stream.WriteByte((byte)count);
            stream.Write(buffer, 0, count);
        }

        protected void WriteInlineString(MemoryStream stream, string text)
        {
            stream.WriteByte(3);
            byte[] bytes = Encoding.UTF8.GetBytes(text);
            stream.Write(bytes, 0, bytes.Length);
            stream.WriteByte(0);
        }
    }
}

